import django_filters
from django.utils import timezone
from .models import Order


class OrderFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=Order.STATUS_CHOICES)
    payment_status = django_filters.ChoiceFilter(choices=Order.PAYMENT_STATUS_CHOICES)
    min_amount = django_filters.NumberFilter(field_name='total_amount', lookup_expr='gte')
    max_amount = django_filters.NumberFilter(field_name='total_amount', lookup_expr='lte')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
    today = django_filters.BooleanFilter(method='filter_today')
    
    class Meta:
        model = Order
        fields = ['status', 'payment_status', 'min_amount', 'max_amount', 
                 'start_date', 'end_date', 'today']
    
    def filter_today(self, queryset, name, value):
        if value:
            return queryset.filter(created_at__date=timezone.now().date())
        return queryset
